#include "MC34671_Handler.h"

uint8 u8isetStatus;
uint16 u16solarCellVoltage;

stateMachine sChargerState;

void vfnMC34671_StateInit (void)
{
    sChargerState.u8prev2State = UNSPECIFIED_STATE;
    sChargerState.u8prevState = UNSPECIFIED_STATE;
    sChargerState.u8currentState = WAIT_FOR_BATTERY;    
}

void vfnMC34671_UpdateStateMachine (void)
{
    sChargerState.u8prev2State = sChargerState.u8prevState;
    sChargerState.u8prevState = sChargerState.u8currentState;
}

uint8 u8MC34671_IsMaxISET (void)
{
    if (u8isetStatus & ISET_600MA)
    {
        return 1;
    }
    return 0;
}

uint8 u8MC34671_IsMinISET (void)
{
    if (u8isetStatus & ISET_250MA)
    {
        return 1;
    }
    return 0;
}

void vfnMC34671_IncreaseISET (void)
{
    if (u8isetStatus & ISET_250MA)
    {
        u8isetStatus = ISET_300MA;
        SWITCH_47K5 = 1;
        SWITCH_8K87 = 0;
    }
    else if (u8isetStatus & ISET_300MA)
    {
        u8isetStatus = ISET_550MA;
        SWITCH_47K5 = 0;
        SWITCH_8K87 = 1;
    }
    else if (u8isetStatus & ISET_550MA)
    {
        u8isetStatus = ISET_600MA;
        SWITCH_47K5 = 1;
        SWITCH_8K87 = 1;  
    }
    else
    {
        return;
    }
}

void vfnMC34671_DecreaseISET (void)
{
    if (u8isetStatus & ISET_300MA)
    {
        u8isetStatus = ISET_250MA;
        SWITCH_47K5 = 0;
        SWITCH_8K87 = 0;
    }
    else if (u8isetStatus & ISET_550MA)
    {
        u8isetStatus = ISET_300MA;
        SWITCH_47K5 = 1;
        SWITCH_8K87 = 0;
    }
    else if (u8isetStatus & ISET_600MA)
    {
        u8isetStatus = ISET_550MA;
        SWITCH_47K5 = 0;
        SWITCH_8K87 = 1;  
    }
    else
    {
        return;
    }
}

void vfnMC34671_StorePanelVoltage (void)
{
    vfnADC_ChannelSelect(ADC_SOLAR_PANEL_CHAN); // Select corresponding ADC chan
    
    while(!u8ADC_CCFCheck()); // Wait for conversion to end
    
    u16solarCellVoltage = u16ADC_GetResult();    
}

uint8 u8MC34671_IsPanelVoltageOK (void)
{
    vfnADC_ChannelSelect (ADC_SOLAR_PANEL_CHAN); // Select corresponding ADC chan
    
    while(!u8ADC_CCFCheck());   // Wait for conversion to end
    
    if (u16ADC_GetResult() > SOLAR_PANEL_VOLTAGE_MIN)
    {
        return 1;
    }
    
    return 0;
}

uint8 u8MC34671_IsBatteryPresent (void)
{
    uint16 u16batVoltage;
    
    vfnADC_ChannelSelect (ADC_BATTERY_CHAN);   // Select corresponding ADC chan
    
    while (!u8ADC_CCFCheck());   // Wait for conversion to end
    
    u16batVoltage = u16ADC_GetResult();
      
    if ((u16batVoltage > VOLTAGE_BAT_MIN) && (u16batVoltage < VOLTAGE_BAT_MAX))
    {
        return 1;
    }
    
    return 0;
}


uint8 u8MC34671_IsIbatConstant (void)
{
    uint16 u16isetVoltage;
    
    vfnADC_ChannelSelect (ADC_MC34671_ISET);   // Select corresponding ADC chan
    
    while (!u8ADC_CCFCheck());   // Wait for conversion to end
    
    u16isetVoltage = u16ADC_GetResult();
    
    if(u16isetVoltage > ISET_1V_VALUE)
    {
        return 1;    
    }
    
    return 0;    
}

uint8 u8MC34671_IsIbatUnderEOC (void)
{
    uint16 u16isetVoltage;
    
    vfnADC_ChannelSelect (ADC_MC34671_ISET);   // Select corresponding ADC chan
    
    while (!u8ADC_CCFCheck());   // Wait for conversion to end
    
    u16isetVoltage = u16ADC_GetResult();
    
    if (u8isetStatus & ISET_600MA)
    {
        u16isetVoltage *= 30;
    }
    else if (u8isetStatus & ISET_550MA)
    {
        u16isetVoltage *= 22;
    }
    else if (u8isetStatus & ISET_300MA)
    {
        u16isetVoltage *= 16;
    }
    else if (u8isetStatus & ISET_250MA)
    {
        u16isetVoltage *= 11;
    }
    
    u16isetVoltage /= 155;
    
    if (u16isetVoltage < 13)
    {
        return 1;
    }
    
    return 0;
}









